<?php
/**
 * Created by PhpStorm.
 * User: sunshine
 * Date: 17-4-18
 * Time: 下午8:45
 */

namespace Admin\Model;


use Think\Model;

class UserCookieModel extends Model{
    protected $tableName = 'user_cookie';

    public function getUidByCookie($userCookie){
        $result =  $this->where('userCookie ="'.(string)$userCookie.'"') -> find();
        if($result){
            return $result['uid'];
        }else{
            return false;
        }
    }

    public function writeCookieInDataBase($uid, $userCookie, $userIp){
        $data = array('userCookie'=>$userCookie,'uid'=>$uid,'userIp'=>$userIp);
        $result = $this->add($data);
        return $result;
    }

    public function logout($userCookie){
        $logoutTime = date('Y-m-d H:i:s');
        $data['userLogoutTime'] = $logoutTime;
        $result = $this->where('userCookie = "'.$userCookie.'"')->save($data);
        if ($result){
            return array(
                'status' => true
            );
        }else{
            return array(
                'status' => false,
                'errcode' => 106,
                'errmsg' => 'write error'
            );
        }
    }
}

class UserModel extends Model
{
    function getUserInfoByCookie($userCookie){
        $usercookiemodel = new UserCookieModel();
        $uid = $usercookiemodel->getUidByCookie($userCookie);
        $returnArr = array();
        if($uid){
            $result = $this->where('uid ='.$uid)->find();
            if($result){
                $returnArr['status'] = true;
                $returnArr['info'] = $result;
            }else{
                $returnArr['status'] = false;
                $returnArr['errcode'] = 101;
                $returnArr['errmsg'] = 'no user';
            }
        }else{
            $returnArr['status'] = false;
            $returnArr['errcode'] = 102;
            $returnArr['errmsg'] = 'no login info';
        }

        return $returnArr;
    }
    //通过用户名获取用户信息,注意权限
    function getUserInfoByUserName($userName, $privilegeCode = 0){
        $returnArr = array();
        if($privilegeCode === 0){
            $result = $this->where('userName = "'.$userName.'"')->find();
            if($result){
                $returnArr['status'] = true;
            }else{
                $returnArr['status'] = false;
            }
        }else if ($privilegeCode === -1){
            $result = $this->where('userName = "'.$userName.'"')->find();
            if($result){
                $returnArr['status'] = true;
                $returnArr['info'] = $result;
            }else{
                $returnArr['status'] = false;
                $returnArr['errcode'] = 101;
                $returnArr['errmsg'] = 'no user';
            }
        }
        return $returnArr;
    }

    function addUser($userName, $password){
        $returnArr = array();
        if($this->where('userName = "'.$userName.'"')->find()){
            $returnArr['status'] = false;
            $returnArr['errcode'] = 105;
            $returnArr['errmsg'] = 'had username';

        }else{
            $pwd = md5($password.'UESTCCCSE');
            $data = array(
                'userName' => $userName,
                'userPassword' => $pwd,
                'userPrivilege' => 1
            );
            $result = $this->add($data);
            if($result){
                $returnArr['status'] = false;
                $returnArr['errcode'] = 106;
                $returnArr['errmsg'] = 'write error';
            }
            $returnArr['status'] = true;
        }
        return $returnArr;


    }
    //登录验证
    function login($userName, $password, $userIp){
        $returnArr = array();
        $result = $this->getUserInfoByUserName($userName,-1);
        if($result['status']){
            $userInfo = $result['info'];
            $pwd = md5($password.'UESTCCCSE');
            if($userInfo['userpassword'] == $pwd){
                $returnArr['status'] = true;
                $cookie = md5($userName.'UESTCCCSE'.$userName.time());
                $usercookiemodel = new UserCookieModel();
                if(!$usercookiemodel->writeCookieInDataBase($userInfo['uid'], $cookie, $userIp)){
                    $returnArr['status'] = false;
                    $returnArr['errcode'] = 106;
                    $returnArr['errmsg'] = 'write error';
                }else{
                    $returnArr['status'] = true;
                    $returnArr['cookie'] = $cookie;
                }
            }else{
                $returnArr['status'] = false;
                $returnArr['errcode'] = 103;
                $returnArr['errmsg'] = 'wrong password';
            }
        }else{
            $returnArr = $result;
        }
        return $returnArr;
    }
    //退出操作
    function logout($userCookie){
        $usercookiemodel = new UserCookieModel();
        return $usercookiemodel->logout($userCookie);
    }
}